package com.iwhalecloud.bss.kite.cucc.service.goods;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.goods.GoodsMemberDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.goods.MemberGoodDTO;
import com.iwhalecloud.bss.kite.cucc.dataservice.cache.CuccOfferMemberCache;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferDetailCache;
import com.iwhalecloud.bss.kite.dataservice.cache.ProdAttrCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Offer;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.OfferDetail;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.ProdAttr;
import com.ztesoft.bss.common.util.ContextUtil;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @ClassName : GoodsHelper
 * @Description :
 * @Author : xie.zhengjun
 * @Date : 2021/3/9 19:56
 */
public class GoodsHelper {
    /**
     * 是否为融合商品
     *
     * @param offerId
     * @return
     */
    public static boolean isGift(String offerId) {
        Offer offer = OfferCache.get(offerId);
        if (Objects.isNull(offer)) {
            return false;
        }
        return KeyConsts.OFFER_TYPE_15.equals(offer.getOfferType());
    }

    /**
     * 是否为实物附加产品
     *
     * @param prodId
     * @return
     */
    public static boolean isMaterialProduct(String prodId) {
        String isMaterialObject = Optional.ofNullable(ProdAttrCache.getByAttrName(prodId, "210004084"))
            .map(ProdAttr::getDefaultValue).orElse(null);
        return KiteStringUtils.isEqual(isMaterialObject, "1_0000106");
    }

    /**
     * 是否为实物附加商品
     * 按要求来，不要乱用，后果自负
     *
     * @param offerId 附加商品的offerId
     * @return
     */
    public static boolean isMaterialOffer(String offerId) {
        List<OfferDetail> offerDetails = OfferDetailCache.get(offerId);
        if (KiteListUtils.isEmpty(offerDetails)) {
            return false;
        }
        for (OfferDetail offerDetail : offerDetails) {
            if (isMaterialProduct(offerDetail.getObjId())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 主商品是否包含实物附加商品
     * 按要求来，不要乱用，后果自负
     *
     * @param mainOfferId 主商品的offerId
     * @return
     */
    public static boolean containMaterialOffer(String mainOfferId) {
        List<GoodsMemberDTO> goodsMembers = CuccOfferMemberCache
            .get(mainOfferId, ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr());
        if (KiteListUtils.isEmpty(goodsMembers)) {
            return false;
        }
        for (GoodsMemberDTO goodsMember : goodsMembers) {
            List<MemberGoodDTO> memberGoods = goodsMember.getMemberGoods();
            if (KiteListUtils.isNotEmpty(memberGoods)) {
                for (MemberGoodDTO memberGood : memberGoods) {
                    if (isMaterialOffer(memberGood.getGoodsSku())) {
                        return true;
                    }
                }

            }
        }
        return false;
    }

    /**
     * 是否为群组商品，如集团短信共享条数和工行短信共享条数
     *
     * @param cateId
     * @return
     */
    public static boolean isGroupGood(String cateId,String offerId) {
        DcPublic dcPublic = DcPublicCache.get("20220104001", cateId);
        if (Objects.isNull(dcPublic)) {
            //是否为主商品，是则返回true
            DcPublic dcPublicNew = DcPublicCache.get("20220104001", offerId);
            if (Objects.isNull(dcPublicNew)) {
                return false;
            }
        }
        return true;
    }

}
